package com.group01.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.SysDictData;
import com.group01.community.entity.SysDictType;
import com.group01.community.service.SysDictTypeService;
import com.group01.community.utils.ExcelUtils;
import com.group01.community.utils.POI;
import com.group01.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Api
@RestController
@RequestMapping("/sysDictType")
public class SysDictTypeController {
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseResult<String> add(@RequestBody SysDictType sysDictType) {
        sysDictType.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        sysDictTypeService.save(sysDictType);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{dictIds}")
    public ResponseResult<String> delete(@PathVariable Long[] dictIds) {
        sysDictTypeService.removeByIds(Arrays.asList(dictIds));
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<String> update(@RequestBody SysDictType sysDictType) {
        sysDictTypeService.updateById(sysDictType);
        return ResponseResult.ok("修改成功");
    }

    @ApiOperation("查询所有")
    @GetMapping
    public ResponseResult<List<SysDictType>> selectAll() {
        return ResponseResult.ok(sysDictTypeService.list(null));
    }

    @ApiOperation("模糊查询")
    @PostMapping("/fuzzy")
    public ResponseResult<IPage<SysDictType>> selectFuzzy(@RequestBody SysDictType sysDictType, Long current, Long size) {
        return ResponseResult.ok(sysDictTypeService.selectFuzzy(sysDictType, current, size));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseResult<IPage<SysDictType>> selectPage(Long current, Long size) {
        return ResponseResult.ok(sysDictTypeService.selectPage(current, size));
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public ResponseResult<SysDictType> selectDictTypeById(@PathVariable Long id) {
        return ResponseResult.ok(sysDictTypeService.getById(id));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseResult<String> export(HttpServletResponse resp) {
        ExcelUtils.export(SysDictType.class, sysDictTypeService, resp, "字典数据信息");
        return ResponseResult.ok("ok");
    }
}

